<template>
  <div v-if="GLOBAL.sysUserVisible=='1'">
    <el-form ref="searchForm"  label-width="20px">

        <el-col :span="2" :offset="22">
          <el-button type="primary" size="mini" @click="showAdd">新增任务</el-button>
        </el-col>


      <el-dialog title="新增任务" align="left" :visible.sync="dialogFormVisible"   :show-close="false">

        <el-form :model="addInfo"  label-width="150px" >
          <el-form-item label="任务标题" >
            <el-input v-model="addInfo.title" ></el-input>
          </el-form-item>
          <el-form-item label="任务内容" prop="tel">
            <el-input v-model="addInfo.content" ></el-input>
          </el-form-item>
          <el-form-item label="选择量表" >
            <el-select v-model="addInfo.scale_id" placeholder="请选择量表">
              <el-option
                v-for="item in sOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属学校" prop="tel">
            <div >&nbsp{{university}}</div>
          </el-form-item>
          <el-form-item label="学院" prop="tel">
            <div >&nbsp{{college}}</div>
          </el-form-item>
          <el-form-item label="入学年份" >
            <div class="block">
<!--              <span class="demonstration">入学年份</span>-->
              <el-date-picker
                v-model="addInfo.grade"
                type="year"
                value-format="yyyy"
                placeholder="请选择入学年份">
              </el-date-picker>
            </div>
          </el-form-item>
        </el-form>
        <div style="margin-top: 100px" slot="footer" class="dialog-footer" align="right">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click=addTask>确 定</el-button>
        </div>
      </el-dialog>

    </el-form>

    <el-table
      stripe
      :row-style="{height:'45px'}"
      :cell-style="{padding:'0px'}"
      style="font-size: 14px;width: 100%"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      :data= taskTable >
      <el-table-column
        prop="index"
        align="center"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        align="center"
        prop="title"
        label="任务标题"
        width="150"
        :show-overflow-tooltip='true' >
      </el-table-column>
      <el-table-column
        align="center"
        prop="content"
        label="任务内容"
        width="200"
        :show-overflow-tooltip='true' >
      </el-table-column>
      <el-table-column
        align="center"
        prop="grade"
        label="入学年份"
        width="150"
        :show-overflow-tooltip='true' >
      </el-table-column>
      <el-table-column
        align="center"
        label="对应量表"
        width="150"
        prop="scale"
        :show-overflow-tooltip='true' >
      </el-table-column>

      <el-table-column
        align="center"
        prop="createTime"
        label="创建时间"
        width="200"
        :show-overflow-tooltip='true' >
      </el-table-column>
<!--      <el-table-column-->
<!--        align="center"-->
<!--        label="任务状态"-->
<!--        width="100"-->
<!--        :show-overflow-tooltip='true' >-->
<!--        <template slot-scope="scope">-->
<!--        <div  class="status" v-if="scope.row.taskStatus == 0">已保存</div>-->
<!--        <div  class="status" v-if="scope.row.taskStatus == 1">已发送</div>-->
<!--        <div  class="status" v-if="scope.row.taskStatus == 2">已撤回</div>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column
        align="center"
        prop="university"
        label="学校名称"
        width="200"
        :show-overflow-tooltip='true' >
      </el-table-column>
      <el-table-column
        align="center"
        prop="college"
        label="学院名称"
        :show-overflow-tooltip='true' >
      </el-table-column>
<!--      <el-table-column  align="center" label="设置">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button class="czBtn" v-if="scope.row.taskStatus != 1" @click="edit(scope.row,1)">发送</el-button>-->
<!--          <el-button class="czBtn" v-if="scope.row.taskStatus == 1" @click="edit(scope.row,2)">撤回</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

  </div>
</template>

<script>
    export default {
        name: "User",
        created() {
            this.initData();
        },
        methods:{
            initData() {
                this.personalInfo()
                this.$axios.post("managementSystem/task/findAll")
                    .then(data => {
                        if (data.status != 200) {
                            this.$message.error('服务出错' + data.status);
                        } else {
                            if (data.data.errorCode == '200') {
                                this.taskTable = data.data.data
                                for (var i in this.taskTable) {
                                    this.taskTable[i]["index"] = parseInt(i) + 1;
                                }
                            }
                        }
                    })
                this.$axios.post("managementSystem/scale/findScaleName")
                    .then(data => {
                        if (data.status != 200) {
                            this.$message.error('服务出错' + data.status);
                        } else {
                            if (data.data.errorCode == '200') {
                                var u = data.data.scaleManagementTable
                                this.sOptions=[]
                                for (var i in u) {
                                    this.sOptions.push({"id":u[i]["id"],"label":u[i]["scaleName"]})
                                }
                            }
                        }
                    })
            },
            personalInfo() {
                this.$axios.post("managementSystem/user/personalInfo").then((res) => {
                    if (res.data.errorCode === 200) {
                        let {  university, college } = res.data.data
                        this.university = university
                        this.college = college
                        if(this.college==null||this.college==""){
                            this.college="全部"
                        }
                    }
                });
            },
            addTask(){
                console.log(this.addInfo.grade)
                  this.$axios.post("managementSystem/task/addTask",this.addInfo)
                      .then(data => {
                          if (data.status != 200) {
                              this.$message.error('服务出错' + data.status);
                          } else {
                              if (data.data.errorCode == '200') {
                                  this.$message({
                                      message: '插入成功',
                                      type: 'success'
                                  });
                              }else{
                                  this.$message.error('插入失败');
                              }
                              this.initData()
                              this.dialogFormVisible = false
                          }
                      });
            },
            edit(row,label){
                this.$axios.post("managementSystem/task/editTask",{"task_id":row.taskId,"label":label})
                    .then(data => {
                        if (data.status != 200) {
                            this.$message.error('服务出错' + data.status);
                        } else {
                            if (data.data.errorCode == '200') {
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                });
                                this.initData()
                            }else{
                                this.$message.error('修改成功');
                            }
                        }
                    });
            },
            showAdd(){
                this.dialogFormVisible = true;
                this.addInfo.title = ""
                this.addInfo.content = ""
                this.addInfo.grade = ""
                this.addInfo.scale_id = null
            },
        },
        data() {
            return {
                taskTable:[],
                addInfo:{
                    title:"",
                    content:"",
                    grade:"",
                    scale_id:null,
                },
                university:"",
                college:"",
                sOptions:[],
                dialogFormVisible:false,
            };
        }
    }
</script>

<style scoped>

</style>
